﻿using ScottPlot.PathStrategies;
using ScottPlot;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MathNet.Numerics;

namespace DetectScars.common
{
    internal class DACUtils
    {
        //获取table数据
        public static List<float[]> GetTableData(TableLayoutPanel table, float long_vel_mm_per_us)
        {
            int count = table.ColumnCount;
            float[][] data = new float[count-1][];
            for (int i = 1; i < count; i++)
            {
                System.Windows.Forms.Control? control1 = table.GetControlFromPosition(i, 0);
                System.Windows.Forms.Control? control2 = table.GetControlFromPosition(i, 1);
                System.Windows.Forms.Control? control3 = table.GetControlFromPosition(i, 2);
                if (control1 == null || control1.Text == "")
                {
                    MessageBox.Show("tcg点声程设置有误");
                    return null;
                }
                if (control2 == null || control2.Text == "")
                {
                    MessageBox.Show("tcg信号值设置有误");
                    return null;
                }
                if (control3 == null || control3.Text == "")
                {
                    MessageBox.Show("tcg增益值设置有误");
                    return null;
                }
                data[i - 1] = new float[3];
                data[i - 1][0] = float.Parse(control1.Text);
                data[i - 1][1] = float.Parse(control2.Text);
                data[i - 1][2] = float.Parse(control3.Text);
            }
            List<float[]> cons = Data2Connect(data, long_vel_mm_per_us);
            return cons;
        }

        //获取table数据
        public static List<float[]> Data2Connect(float[][] tcg,float sd)
        {
            float[][] cons = new float[tcg.Length][];
            float cz =  3;
            for(int i = 0; i < tcg.Length; i++)
            {
                cons[i] = new float[6];
                cons[i][0] = 200 * tcg[i][0] / sd;
                cons[i][1] = tcg[i][2];
                cons[i][4] = tcg[i][0];
                cons[i][5] = tcg[i][1];
                if (i == 0)
                {
                    cons[i][2] = 0;
                    cons[i][3] = 0;
                }
                else if (i == tcg.Length - 1)
                {
                    cons[i - 1][3] = (cons[i][0] - cons[i - 1][0]) / 100 - cz;
                    cons[i - 1][2] = Math.Abs(tcg[i - 1][2] - tcg[i][2]) * 10 / cons[i - 1][3];
                    cons[i][2] = cons[i - 1][3];
                    cons[i][3] = cons[i - 1][2];
                }
                else
                {
                    cons[i-1][3] = (cons[i][0] - cons[i - 1][0]) / 100- cz;
                    cons[i-1][2] = Math.Abs(tcg[i - 1][2] - tcg[i][2])*10 / cons[i - 1][3];
                    
                }
            }
            List<float[]> conns = cons.ToList();
            return conns;
        }

        //获取table数据
        public static List<float[]> AddTcgData(List<float[]> list, ushort[] tcg, float sd)
        {
            float[] con = new float[6];
            if (list.Count == 0)
            {
                con[0] = 200 * tcg[0] / sd;
                con[1] = tcg[2];
                con[2] = 0;
                con[3] = 0;
                con[4] = tcg[0];
                con[5] = tcg[1];
            }
            else
            {
                con[0] = 200 * tcg[0] / sd;
                con[1] = tcg[2];
                con[4] = tcg[0];
                con[5] = tcg[1];
                list[list.Count-1][3] = (con[0] - list[list.Count - 1][0]) / 100;
                list[list.Count - 1][2] = Math.Abs(list[list.Count - 1][1] - tcg[2]) / con[3];
                con[2] = list[list.Count - 1][2];
                con[3] = list[list.Count - 1][3];
                
            }
            list.Add(con);
            return list;
        }

        //获取table数据
        public static float[][] Connect2Data(List<float[]> cons)
        {
            float[][] tcg = new float[cons.Count][];
            for (int i = 0; i < cons.Count; i++)
            {
                tcg[i] = new float[3];
                tcg[i][0] = cons[i][4];
                tcg[i][1] = cons[i][5];
                tcg[i][2] = cons[i][1];
            }
            return tcg;
        }


        //展示TCG数据
        public static void ShowTable(TableLayoutPanel table, List<float[]> cons)
        {
            table.Controls.Clear();
            if (cons==null || cons.Count==0)
            {
                //初始化
                int rows = 3;
                int cols = 2;
                table.RowCount = rows;
                table.ColumnCount = cols;
                TextBox textBox1 = new TextBox
                {
                    Dock = DockStyle.Fill,
                    Text = "声程",
                    Width = 50,
                    TextAlign = System.Windows.Forms.HorizontalAlignment.Center
                };
                TextBox textBox2 = new TextBox
                {
                    Dock = DockStyle.Fill,
                    Text = "信号值",
                    Width = 50,
                    TextAlign = System.Windows.Forms.HorizontalAlignment.Center
                };
                TextBox textBox3 = new TextBox
                {
                    Dock = DockStyle.Fill,
                    Text = "增益",
                    Width = 50,
                    TextAlign = System.Windows.Forms.HorizontalAlignment.Center
                };
                table.Controls.Add(textBox1, 0, 0);
                table.Controls.Add(textBox2, 0, 1);
                table.Controls.Add(textBox3, 0, 2);
                table.ColumnStyles[0].Width = 30;
                table.ColumnStyles[1].Width = 30;
            }
            else
            {
                float[][] data = Connect2Data(cons);
                int count = data.Length;
                //初始化dac
                table.RowCount = 3;
                table.ColumnCount = count + 1;
                TextBox textBox1 = new TextBox
                {
                    Dock = DockStyle.Fill,
                    Text = "声程",
                    TextAlign = System.Windows.Forms.HorizontalAlignment.Center
                };
                TextBox textBox2 = new TextBox
                {
                    Dock = DockStyle.Fill,
                    Text = "信号值",
                    TextAlign = System.Windows.Forms.HorizontalAlignment.Center
                };
                TextBox textBox3 = new TextBox
                {
                    Dock = DockStyle.Fill,
                    Text = "增益",
                    TextAlign = System.Windows.Forms.HorizontalAlignment.Center
                };
                table.Controls.Add(textBox1, 0, 0);
                table.Controls.Add(textBox2, 0, 1);
                table.Controls.Add(textBox3, 0, 2);
                table.ColumnStyles[0].Width = 100;
                for (int col = 0; col < count; col++)
                {

                    TextBox textBox1_ = new TextBox
                    {
                        Dock = DockStyle.Fill,
                        Text = data[col][0].ToString(),
                        TextAlign = System.Windows.Forms.HorizontalAlignment.Center
                    };
                    TextBox textBox2_ = new TextBox
                    {
                        Dock = DockStyle.Fill,
                        Text = data[col][1].ToString(),
                        TextAlign = System.Windows.Forms.HorizontalAlignment.Center
                    };
                    TextBox textBox3_ = new TextBox
                    {
                        Dock = DockStyle.Fill,
                        Text = data[col][2].ToString(),
                        TextAlign = System.Windows.Forms.HorizontalAlignment.Center
                    };
                    table.Controls.Add(textBox1_, col + 1, 0);
                    table.Controls.Add(textBox2_, col + 1, 1);
                    table.Controls.Add(textBox3_, col + 1, 2);
                    table.ColumnStyles[col+1].Width = 100;
                }
            }
            
        }

        /// <summary>
        /// 将 n(≤10) 个 2D 点平滑拟合成 512 个点。
        /// </summary>
        /// <param name="src">原始点集，长度 ≤10。</param>
        /// <param name="count">输出点数，默认 512。</param>
        /// <returns>512 个平滑点。</returns>
        public static (double[] x, double[] y) Fit512((double x, double y)[] src, int count = 512)
        {

            // 1) 生成三次均匀 B 样条插值器
            var spline = MathNet.Numerics.Interpolation.CubicSpline.InterpolateNatural(
                Enumerable.Range(0, src.Length).Select(i => (double)i).ToArray(),
                src.Select(p => p.x).ToArray());

            var splineY = MathNet.Numerics.Interpolation.CubicSpline.InterpolateNatural(
                Enumerable.Range(0, src.Length).Select(i => (double)i).ToArray(),
                src.Select(p => p.y).ToArray());

            // 2) 在参数空间 [0, n-1] 上均匀采样 512 份
            double[] t = MathNet.Numerics.Generate.LinearSpaced(count, 0, src.Length - 1);

            // 3) 计算曲线坐标
            double[] xOut = t.Select(spline.Interpolate).ToArray();
            double[] yOut = t.Select(splineY.Interpolate).ToArray();

            return (xOut, yOut);
        }
        //tcg转dac
        public static (double[], float[]) Tcg2Dac(List<float[]> cons,float base_zy,int a,int b)
        {
            double[] x_ = new double[cons.Count];
            double[] y_ = new double[cons.Count];
            for (int i = 0; i < cons.Count; i++)
            {
                x_[i] = cons[i][4];
                y_[i] = cons[i][5] * Math.Pow(10,(base_zy - cons[i][1]) /20);
            }
            int newCount = 512;
            var(xnew, ynew) = OtherUtils.GeneratePoints(x_, y_, a, b, newCount);
            return (xnew, ynew);
        }
    }
}
